package cn.b.sky.shop.activiti.service.impl;

import cn.b.sky.shop.activiti.dao.FlowNodeDao;
import cn.b.sky.shop.activiti.dao.FlowNodeEventDao;
import cn.b.sky.shop.activiti.dao.FlowNodeUserDao;
import cn.b.sky.shop.activiti.enums.FlowNodeUserEnum;
import cn.b.sky.shop.activiti.model.FlowNode;
import cn.b.sky.shop.activiti.model.FlowNodeEvent;
import cn.b.sky.shop.activiti.model.FlowNodeUser;
import cn.b.sky.shop.activiti.service.CustomGroupAndRoleService;
import cn.b.sky.shop.activiti.service.FlowNodeService;
import cn.b.sky.user.service.UserService;
import cn.b.sky.user.service.model.UserInfo;
import org.activiti.engine.impl.bpmn.parser.BpmnParse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("flowNodeService")
public class FlowNodeServiceImpl implements FlowNodeService {

  @Autowired
  private FlowNodeDao flowNodeDao;

  @Autowired
  private FlowNodeUserDao flowNodeUserDao;

  @Autowired
  private FlowNodeEventDao flowNodeEventDao;

  @Resource
  private CustomGroupAndRoleService customGroupAndRoleService;

  @Resource
  private UserService userService;

  @Override
  public List<FlowNodeUser> getFlowNodeUserByFlowNode(FlowNode flowNode) {
    if (flowNode == null || flowNode.getId() == null) {
      return null;
    }
    List<FlowNodeUser> nodeUsers = flowNodeUserDao.selectFlowNodeUserByFlowNodeId(flowNode.getId());
    for (FlowNodeUser userNode : nodeUsers) {
      String value = userNode.getValue();
      if(StringUtils.isBlank(value)){
        continue;
      }
      if (FlowNodeUserEnum.ASSIGNEE.getCode().equals(userNode.getType())) {
        if (value.indexOf("${") == -1) {
          UserInfo userInfo = userService.selectUser(flowNode.getTenantId(), userNode.getValue());
          if (userInfo != null) {
            userNode.setAssigneeName(userInfo.getUserText());
          }
        }else{
          userNode.setAssigneeName(value);
        }
      } else if (FlowNodeUserEnum.CANDIDATEGROUPS.getCode().equals(userNode.getType())) {

        if (!StringUtils.isBlank(value) && value.indexOf("${") == -1) {
           userNode.setGroups(customGroupAndRoleService.findGroupsTextByCode(value, flowNode.getTenantId()));
           userNode.setRoles(customGroupAndRoleService.findRolesTextByCode(value, flowNode.getTenantId()));
        }else{
          userNode.setGroups(value);
        }
      } else if (FlowNodeUserEnum.CANDIDATEUSERS.getCode().equals(userNode.getType())) {
        if(BpmnParse.PROPERTYNAME_INITIATOR_VARIABLE_NAME.equals(value)){
          userNode.setAssigneeName("流程发起人");
        }else{
          if(value.indexOf("${") == -1){
            userNode.setAssigneeName(customGroupAndRoleService.findUsersTextByCode(value, flowNode.getTenantId()));
          }else{
            userNode.setAssigneeName(value);
          }      
        }
      }
    }
    return nodeUsers;
  }

  @Override
  public List<FlowNodeEvent> getFlowNodeEventByFlowNode(FlowNode flowNode) {

    if (flowNode == null || flowNode.getId() == null) {
      return null;
    }
    return flowNodeEventDao.selectFlowNodeEventByFlowNodeId(flowNode.getId());
  }

  @Override
  public FlowNode getFlowNodeByModeIdAndActivitId(String modelId, String activitId, String tenantId) {

    return flowNodeDao.selectFlowNodeByModelIdAndCode(modelId, tenantId, activitId);
  }

  @Override
  public FlowNode getFlowNodeByFlowNodeId(Long flowNodeId) {
    if (flowNodeId != null) {
      return flowNodeDao.selectFlowNodeByFlowNodeId(flowNodeId);
    }
    return null;
  }

  @Override
  public void modifyFlowNodeUser(FlowNodeUser flowNodeUser) {
    flowNodeUserDao.updateFlowNodeUser(flowNodeUser);

  }

  @Override
  public void modifyFlowNodeEvent(FlowNodeEvent flowNodeEvent) {

  }

  @Override
  public void saveFlowNode(FlowNode flowNode) {
    this.flowNodeDao.saveFlowNode(flowNode);
  }

  
  @Override
  public void modifyFlowNode(FlowNode flowNode) {
    this.flowNodeDao.updateFlowNode(flowNode);
    
  }

  @Override
  public void saveFlowNodeUser(FlowNodeUser flowNodeUser) {
    if (flowNodeUser != null && flowNodeUser.getFlowNodeId() != null) {
      flowNodeUserDao.insertFlowNodeUser(flowNodeUser);
    }

  }

  @Override
  public void saveFlowNodeEvent(FlowNodeEvent flowNodeEvent) {
    if (flowNodeEvent != null && flowNodeEvent.getFlowNodeId() != null) {
      flowNodeEventDao.insertFlowNodeEvent(flowNodeEvent);
    }

  }

  @Override
  public void deleteFlowNodeUserById(Long id) {
    if (id != null) {
      flowNodeUserDao.deleteFlowNodeUserById(id);
    }

  }

  @Override
  public void deleteFlowNodeEventById(Long id) {
    if (id != null) {
      flowNodeEventDao.deleteFlowNodeEventById(id);
    }

  }

  @Override
  public void batchSaveFlowNodeUser(List<FlowNodeUser> flowNodeUsers, String formKey, String nodeId) {
    if (flowNodeUsers != null && flowNodeUsers.size() > 0) {
      for (FlowNodeUser user : flowNodeUsers) {
/*        //ASSIGNEE类型特殊对待,如果此节点已经配置了人员（角色或组织），则把类型设置为CANDIDATEUSERS
        if(FlowNodeUserEnum.ASSIGNEE.getCode().equals(user.getType())){
          List<FlowNodeUser> nodeUsers = flowNodeUserDao.selectFlowNodeUserByFlowNodeId(user.getFlowNodeId());
          if(nodeUsers !=null){
            user.setType(FlowNodeUserEnum.CANDIDATEUSERS.getCode());
          }
        }*/
        //修改保存
        FlowNodeUser nodeUser = flowNodeUserDao.selectFlowNodeUserByCondition(user);
        if (nodeUser != null && nodeUser.getId() != null) {
          String value = nodeUser.getValue();
          //非表达式并且类型不是 assignee（执行人）的话就累积相加，否则就是替换
          if (value.indexOf("${") == -1 && !FlowNodeUserEnum.ASSIGNEE.getCode().equals(nodeUser.getType())) {
            //去重复
            if(user.getValue().indexOf(value) == -1){
              user.setValue(user.getValue() + "," + value);
            }
          }
          user.setId(nodeUser.getId());
          this.modifyFlowNodeUser(user);
        } else {
          this.saveFlowNodeUser(user);
        }
      }
    }
    //修改formkey
    if(!StringUtils.isBlank(formKey) && !StringUtils.isBlank(nodeId)){
      FlowNode node = new FlowNode();
      node.setFormKey(formKey);
      node.setId(Long.valueOf(nodeId));
      this.modifyFlowNode(node);
    }
  }

}
